package com.buaa.pm.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RequestUtils {
    /**
     * HttpPost请求，使用formdata
     * @param url
     * @return
     * @throws Exception
     */
    public static Object requestPostUrl(String url, Map<String, String> param) throws Exception{

        InputStream is = null;
        String body = null;
        StringBuilder res = new StringBuilder();
        HttpPost httpPost = new HttpPost(url);


        // 设置请求的参数
        List<NameValuePair> params = new ArrayList<>();
        for (Map.Entry<String, String> entry : param.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(params));


        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            is = entity.getContent();
            System.out.println(entity);
            //转换为字节输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
            while((body=br.readLine()) != null){
                res.append(body);
            }
        }
        Object jsonMap = JSON.parse(res.toString());
        return jsonMap;
    }

    /**
     * HttpPost请求，使用json请求体
     * @param url
     * @return
     * @throws Exception
     */
    public static Object requestPostUrlByJson(String url, Map<String, Object> param) throws Exception{

        InputStream is = null;
        String body = null;
        StringBuilder res = new StringBuilder();
        HttpPost httpPost = new HttpPost(url);

        // 设置请求的参数
        String jsonBody = JSON.toJSONString(param);

        StringEntity stringEntity = new StringEntity(jsonBody, "utf-8");

        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            is = entity.getContent();
            //转换为字节输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
            while((body=br.readLine()) != null){
                res.append(body);
            }
        }
        Object jsonMap = JSON.parse(res.toString());
        return jsonMap;
    }
}
